/*******************************************************************************
 * Copyright (c) 2011 Prometheus Consulting
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
/**
 * 
 */
package nz.co.senanque.vaadinsupport.hibernatecontainer;

import java.util.Properties;

import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.TransactionProxyFactoryBean;


/**
 * This wraps a Transactional Proxy around the target object passed to us.
 * The interesting this is that the target is not instantiated by Spring.
 * 
 * @author Roger Parkinson
 *
 */
public class DynamicTransactionalProxyFactory {
	
	private static Logger logger = LoggerFactory.getLogger(DynamicTransactionalProxyFactory.class);
	private SessionFactory m_sessionFactory;
	private PlatformTransactionManager m_transactionManager;
	private Properties m_transactionAttributes;

	public Object getProxy(SessionFactoryHolder target)
	{
		TransactionProxyFactoryBean transactionProxyFactoryBean = new TransactionProxyFactoryBean();
		transactionProxyFactoryBean.setTarget(target);
		transactionProxyFactoryBean.setTransactionManager(getTransactionManager());
		transactionProxyFactoryBean.setTransactionAttributes(getTransactionAttributes());
		transactionProxyFactoryBean.afterPropertiesSet();
		SessionFactoryHolder ret = (SessionFactoryHolder)transactionProxyFactoryBean.getObject();
		ret.setSessionFactory(getSessionFactory());
		return ret;
	}

	public SessionFactory getSessionFactory() {
		return m_sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		m_sessionFactory = sessionFactory;
	}

	public PlatformTransactionManager getTransactionManager() {
		return m_transactionManager;
	}

	public void setTransactionManager(PlatformTransactionManager transactionManager) {
		m_transactionManager = transactionManager;
	}

	public Properties getTransactionAttributes() {
		return m_transactionAttributes;
	}

	public void setTransactionAttributes(Properties transactionAttributes) {
		m_transactionAttributes = transactionAttributes;
	}

}
